package com.example.a_java.controller.monitor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.a_java.common.Result;
import com.example.a_java.entity.SysOperLog;
import com.example.a_java.service.SysOperLogService;
import com.example.a_java.util.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 操作日志Controller
 */
@Tag(name = "操作日志", description = "操作日志相关接口")
@RestController
@RequestMapping("/monitor/operLog")
@SecurityRequirement(name = "bearer-key")
public class SysOperLogController {

    @Autowired
    private SysOperLogService operLogService;

    /**
     * 获取操作日志列表
     */
    @Operation(summary = "获取操作日志列表")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('monitor:operlog:list')")
    public Result<Page<SysOperLog>> list(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "操作模块") @RequestParam(required = false) String module,
            @Parameter(description = "操作类型") @RequestParam(required = false) String operType,
            @Parameter(description = "操作状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "操作人用户名") @RequestParam(required = false) String username,
            @Parameter(description = "开始时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @Parameter(description = "结束时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        
        Page<SysOperLog> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SysOperLog> wrapper = new LambdaQueryWrapper<>();
        
        wrapper.like(StringUtils.hasText(module), SysOperLog::getModule, module)
                .like(StringUtils.hasText(operType), SysOperLog::getOperType, operType)
                .eq(status != null, SysOperLog::getStatus, status)
                .like(StringUtils.hasText(username), SysOperLog::getUsername, username)
                .ge(startTime != null, SysOperLog::getOperTime, startTime)
                .le(endTime != null, SysOperLog::getOperTime, endTime)
                .orderByDesc(SysOperLog::getOperTime);
        
        operLogService.page(page, wrapper);
        return Result.success(page);
    }
    
    /**
     * 导出操作日志
     */
    @Operation(summary = "导出操作日志")
    @GetMapping("/export")
    @PreAuthorize("hasAuthority('monitor:operlog:export')")
    public void export(
            HttpServletResponse response,
            @Parameter(description = "操作模块") @RequestParam(required = false) String module,
            @Parameter(description = "操作类型") @RequestParam(required = false) String operType,
            @Parameter(description = "操作状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "操作人用户名") @RequestParam(required = false) String username,
            @Parameter(description = "开始时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @Parameter(description = "结束时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        
        LambdaQueryWrapper<SysOperLog> wrapper = new LambdaQueryWrapper<>();
        
        wrapper.like(StringUtils.hasText(module), SysOperLog::getModule, module)
                .like(StringUtils.hasText(operType), SysOperLog::getOperType, operType)
                .eq(status != null, SysOperLog::getStatus, status)
                .like(StringUtils.hasText(username), SysOperLog::getUsername, username)
                .ge(startTime != null, SysOperLog::getOperTime, startTime)
                .le(endTime != null, SysOperLog::getOperTime, endTime)
                .orderByDesc(SysOperLog::getOperTime);
        
        List<SysOperLog> list = operLogService.list(wrapper);
        
        String[] headers = {"日志编号", "操作模块", "操作类型", "操作描述", "请求方法", "操作人员", "操作IP", "操作状态", "操作时间"};
        String[] fields = {"operId", "module", "operType", "operDesc", "requestMethod", "username", "operIp", "status", "operTime"};
        
        ExcelUtil.exportExcel(response, "操作日志.xlsx", "操作日志", list, headers, fields);
    }

    /**
     * 删除操作日志
     */
    @Operation(summary = "删除操作日志")
    @DeleteMapping("/{operIds}")
    @PreAuthorize("hasAuthority('monitor:operlog:delete')")
    public Result<Boolean> delete(@PathVariable Long[] operIds) {
        return Result.success(operLogService.removeByIds(java.util.Arrays.asList(operIds)));
    }

    /**
     * 清空操作日志
     */
    @Operation(summary = "清空操作日志")
    @DeleteMapping("/clean")
    @PreAuthorize("hasAuthority('monitor:operlog:delete')")
    public Result<Boolean> clean() {
        operLogService.cleanOperLog();
        return Result.success(true);
    }
} 
